<?php
class hello_model extends CI_Model
{
	public $table_name='hello';
	function __construct()
	{
		parent::__construct();
		$config['hostname'] = 'localhost';
		$config['username'] = 'root';
		$config['password'] = '';
		$config['database'] = 'helloworld';
		$config['dbdriver'] = 'mysql';
		$config['dbprefix'] = '';
		$config['pconnect'] = TRUE;
		$config['db_debug'] = TRUE;
		$config['cache_on'] = FALSE;
		$config['cachedir'] = '';
		$config['char_set'] = 'utf8';
		$config['dbcollat'] = 'utf8_general_ci';
		$config['swap_pre'] = '';
		$config['autoinit'] = TRUE;
		$config['stricton'] = FALSE;
		//@@@@@@testers: check the database name before start!!
		$this->load->database($config);
	}
	
	
	//full functionality, @@required field name to be matched@@
	function create()
	{
		$new_hello['userid'] = $this->input->post('userid');
		$new_hello['place'] = $this->input->post('place');
		$new_hello['time'] = $this->input->post('time');
		$new_hello['title'] = $this->input->post('title');
		$new_hello['place'] = $this->input->post('place');
		$new_hello['isconfirmed'] = false;
		
		$insert = $this->db->insert($this->table_name, $new_hello);
		return $insert;

	}
	
	//not yet being used
	function update($profile)
	{
		$this->db->where('helloid', $profile['helloid']);
		$this->db->update($this->table_name, $profile);
	}
	//not yet being used
	function delete($profile)
	{
		$this->db->where('helloid', $profile['helloid']);
		$this->db->delete($this->table_name, $profile);
	}
	
	//@@@still need consideration
	// not yet being used
	function get_hello($profile)
	{
		reset($profile);

		while (list($field, $content) = each($profile))
		{
			$this->db->where($field, $content);
		}
		return $this->db->get($this->table_name);
		
	}
	
	//As the name says, I do not know I ever need it
	function ChangeQueryResultToArray($query)
	{
		if($query->num_rows() > 0)
		{
			foreach ($query->result() as $row)
			{
				$data[] = $row;
			}
			return $data;
		}
	}
	
	// this may no longer be useful, even for test
	function getAll()//For test use Only
	{
		
		return $this->db->get($this->table_name);
	}
	function get_all_with_fullname()
	{
		$this->db->select('hello.helloid,hello.title,hello.place,hello.time, user.fullname');
		$this->db->from('user');
		$this->db->join('hello', 'hello.userid = user.userid');
		return $this->db->get();
	}
}
?>
